//package com.huangjw.lbr.gateway.filter;
//
//import cn.hutool.core.codec.Base64;
//import cn.hutool.core.util.CharsetUtil;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.crypto.Mode;
//import cn.hutool.crypto.Padding;
//import cn.hutool.crypto.symmetric.AES;
//import cn.hutool.http.HttpUtil;
//import com.ares.common.core.constant.SecurityConstants;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.util.UriComponentsBuilder;
//import reactor.core.publisher.Mono;
//
//import javax.crypto.spec.IvParameterSpec;
//import javax.crypto.spec.SecretKeySpec;
//import java.net.URI;
//import java.nio.charset.StandardCharsets;
//import java.util.Map;
//
///**
// * 密码解密工具类
// * @author Administrator
// * @date 2020/12/28 10:46
// * @since <版本号>
// */
//@Slf4j
//@Component
//public class PasswordDecoderFilter extends AbstractGatewayFilterFactory {
//	private static final String PASSWORD = "password";
//	private static final String KEY_ALGORITHM = "AES";
//    /**
//     * 配置文件加密key,要和前端配置一致
//     */
//	@Value("${security.encode.key:1234567812345678}")
//	private String encodeKey;
//
//	@SneakyThrows
//	private static String decryptAES(String data, String encodeKey) {
//		AES aes = new AES(Mode.CBC, Padding.NoPadding,
//				new SecretKeySpec(encodeKey.getBytes(), KEY_ALGORITHM),
//				new IvParameterSpec(encodeKey.getBytes()));
//		byte[] result = aes.decrypt(Base64.decode(data.getBytes(StandardCharsets.UTF_8)));
//		return new String(result, StandardCharsets.UTF_8);
//	}
//
//	@Override
//	public GatewayFilter apply(Object config) {
//		return (exchange, chain) -> {
//			ServerHttpRequest request = exchange.getRequest();
//
//			// 不是登录请求，直接向下执行
//			if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath(), SecurityConstants.OAUTH_TOKEN_URL)) {
//				return chain.filter(exchange);
//			}
//
//			URI uri = exchange.getRequest().getURI();
//			String queryParam = uri.getRawQuery();
//			Map<String, String> paramMap = HttpUtil.decodeParamMap(queryParam, CharsetUtil.UTF_8);
//
//			String password = paramMap.get(PASSWORD);
//			if (StrUtil.isNotBlank(password)) {
//				try {
//					password = decryptAES(password, encodeKey);
//				} catch (Exception e) {
//					log.error("密码解密失败:{}", password);
//					return Mono.error(e);
//				}
//				paramMap.put(PASSWORD, password.trim());
//			}
//
//			URI newUri = UriComponentsBuilder.fromUri(uri)
//				.replaceQuery(HttpUtil.toParams(paramMap))
//				.build(true)
//				.toUri();
//
//			ServerHttpRequest newRequest = exchange.getRequest().mutate().uri(newUri).build();
//			return chain.filter(exchange.mutate().request(newRequest).build());
//		};
//	}
//
//    public static void main(String[] args) {
//        String data = "L3On2xLIXd4QSwXFejKKEw==";
//        String key = "aresgqposarespos";
//        System.out.println(decryptAES(data,key));
////        String data = "rKu1/348LvKp0rsVC06eCA==";
////        String key = "pigxpigxpigxpigx";
////        System.out.println(decryptAES(data,key));
//    }
//}
//
